package com.fooddelivery.Service.Impl;

import com.fooddelivery.Entity.Category;
import com.fooddelivery.Entity.Dish;
import com.fooddelivery.Mapper.CategoryMapper;
import com.fooddelivery.Mapper.DishMapper;
import com.fooddelivery.Service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author zhengruiyang
 * @since 2025-08-31
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Category category) {
        try {
            Dish dish = new Dish();
            dish.setCategoryId(category.getId());
            dishMapper.removeByCategoryId(dish);
            categoryMapper.removeById(category);
            return true;
        } catch (Exception e) {
            // 记录日志
            log.error("删除分类及关联菜品失败", e);
            // 抛出异常，触发事务回滚
            throw new RuntimeException("删除分类失败", e);
        }
    }
}
